//
//  MineInfoEditNewCollectionViewCell.m
//  MJApp
//
//  Created by 王亮 on 15/7/23.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "MineInfoEditNewCollectionViewCell.h"
#import "MineInfoEditNewTableViewCell.h"

@interface MineInfoEditNewCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>
{
    CGFloat _cellHigh;
    NSMutableArray *_nameArray;
    NSMutableArray *_descArray;
}

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MineInfoEditNewCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        [self prepareData];
        [self UI];
    }
    return self;
}

- (void)UI {
    
    _cellHigh = CGRectGetHeight(self.frame) / 8.0f;
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.separatorColor = [UIColor clearColor];
    tableView.delegate = self;
    tableView.dataSource = self;
    [self addSubview:tableView];
    self.tableView = tableView;
    
    self.layer.cornerRadius = 5;
    self.layer.borderColor = [UIColor grayColor].CGColor;
    self.layer.borderWidth = 0.5;
    
}
- (void)prepareData {
    
    _nameArray = [NSMutableArray array];
    [_nameArray addObject:@"*昵称"];
    [_nameArray addObject:@"*年龄"];
    [_nameArray addObject:@"*性别"];
    [_nameArray addObject:@"生日"];
    [_nameArray addObject:@"邮箱"];
    [_nameArray addObject:@"QQ"];
    [_nameArray addObject:@"所在地"];
    [_nameArray addObject:@"个人签名"];
    _descArray = [NSMutableArray array];
}

- (void)setInfoDic:(NSDictionary *)infoDic {
    
    if ([infoDic allKeys].count == 0) {
        return;
    }
    _infoDic = infoDic;
    [_descArray removeAllObjects];
    [_descArray addObject:@{@"key":@"nick_name",@"value":infoDic[@"nick_name"]}];
    [_descArray addObject:@{@"key":@"age",@"value":infoDic[@"age"]}];
    NSString *sexName = [infoDic[@"sex"] integerValue] == 1 ? @"男" : @"女";
    [_descArray addObject:@{@"key":@"sex",@"value":sexName}];
    [_descArray addObject:@{@"key":@"birthday",@"value":infoDic[@"birthday"]}];
    [_descArray addObject:@{@"key":@"email",@"value":infoDic[@"email"]}];
    [_descArray addObject:@{@"key":@"qq",@"value":infoDic[@"qq"]}];
    [_descArray addObject:@{@"key":@"address",@"value":infoDic[@"address"]}];
    [_descArray addObject:@{@"key":@"remark",@"value":infoDic[@"remark"]}];
    [self.tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 8;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return _cellHigh;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *cellID = @"MineInfoEditNewTableViewCell";
    MineInfoEditNewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        
        cell = WL_XIB(@"MineInfoEditNewTableViewCell");
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    BOOL isShow = indexPath.row == 7 ? YES : NO;
    dic[@"hidden"] = @(isShow);
    dic[@"name"] = _nameArray[indexPath.row];
    BOOL isArrowShow = YES;
    if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row == 6) {
        
        isArrowShow = NO;
    }
    dic[@"arrowShow"] = @(isArrowShow);
  
    if (self.infoDic != nil) {
        
        if (indexPath.row == 6) {
            
            dic[@"areaList"] = self.infoDic[@"areaList"];
        }
        dic[@"desc"] = _descArray[indexPath.row];
    }
    cell.infoDic = dic;
    return cell;
}

- (NSDictionary *)valueDic {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    for (int i = 0; i < _nameArray.count; i ++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        MineInfoEditNewTableViewCell *cell = (MineInfoEditNewTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
        dic[cell.infoDic[@"desc"][@"key"]] = cell.descTextField.text;
    }
    return dic;
}

@end
